#!D:/CODE/python
# -*- coding: utf-8 -*-
# @Time : 2020/6/20 19:59
# @Author : Alexdong
# @Site : 
# @File : 字符串离散化.py
# @Software: PyCharm
# Functional description:
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np

file_path = ".csv"
df = pd.read_csv(file_path)

print(df.info())

print(df.head(1))  # 第一行

print(df["Genre"])

# 统计分类的列表
temp_list = df['Genre'].str.split(",").tolist()  # [[],[]]
genre_list = list(set([i for j in temp_list for i in j]))

# 构造全为0的数组
zeros_df = pd.DataFrame(np.zeros((df.shape[0], len(genre_list))), columns=genre_list)
print(zeros_df)

# 给每个电影出现分类的位置赋值1
for i in range(df.shape[0]):
    # zeros_df.loc[0, ["ser","mud"]] =1
    zeros_df.loc[i, temp_list[i]] = 1

print(zeros_df.head(3))
# 统计每列的个数，求和，每个电影分类的总数
gener_count = zeros_df.sum(axis= 0)
print(gener_count)

# 排序
gener_count = gener_count.sort_values()

# 画图
_x = gener_count.index
_y = gener_count.sort_values
plt.figure(figsize=(20, 8), dpi=80)
plt.bar(range(len(_x)), _y)
plt.xticks(range(len(_x)), _x)

